home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / src / java / net / datagr~1.jav < prev    next >
Encoding:
Text File  |  1996-01-12  |  4.1 KB  |  139 lines

  1. /*
  2.  * @(#)DatagramSocket.java    1.9 96/01/11 Pavani Diwanji
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.net;
  21.  
  22. import java.io.FileDescriptor;
  23. import java.io.IOException;
  24. import java.io.InterruptedIOException;
  25.  
  26. /**
  27.  * The datagram socket class implements unreliable datagrams.
  28.  * @author Pavani Diwanji
  29. */
  30. public
  31. class DatagramSocket {
  32.     private int localPort;
  33.     private FileDescriptor fd;
  34.  
  35.     /**
  36.      * Load net library into runtime.
  37.      */
  38.     static {
  39.     System.loadLibrary("net");
  40.     }
  41.  
  42.     /**
  43.      * Creates a datagram socket
  44.      */
  45.     public DatagramSocket() throws SocketException {
  46.         fd = new FileDescriptor();
  47.     // creates a udp socket
  48.     datagramSocketCreate();
  49.     // binds the udp socket to any local available port
  50.     localPort = datagramSocketBind(0);
  51.     }
  52.  
  53.     /**
  54.      * Creates a datagram socket
  55.      * @param local port to use
  56.      */
  57.     public DatagramSocket(int port) throws SocketException {
  58.     SecurityManager security = System.getSecurityManager();
  59.     if (security != null) {
  60.         security.checkListen(port);
  61.     }
  62.         fd = new FileDescriptor();
  63.     // creates a udp socket
  64.     datagramSocketCreate();
  65.     // binds the udp socket to desired port
  66.     localPort = datagramSocketBind(port);
  67.     }
  68.  
  69.     /**
  70.      * Sends Datagram Packet to the destination address
  71.      * @param DatagramPacket to be sent. The packet contains the buffer 
  72.      * of bytes, length and destination InetAddress and port.
  73.      * @exception IOException i/o error occurred
  74.      */
  75.     public void send(DatagramPacket p) throws IOException  {
  76.  
  77.     // check the address is ok wiht the security manager on every send.
  78.     SecurityManager security = System.getSecurityManager();
  79.     if (security != null) {
  80.         security.checkConnect(p.getAddress().getHostName(), p.getPort());
  81.     }
  82.  
  83.     // call the native method to send
  84.     datagramSocketSend(p);
  85.     }
  86.  
  87.     /**
  88.      * Receives datagram packet.
  89.      * @param DatagramPacket to be received.
  90.      * On return, the DatagramPacket contains the buffer in which the 
  91.      * data is received, packet length, sender's address and sender's port 
  92.      * number. Blocks until some input is available.
  93.      * @exception IOException i/o error occurred
  94.      */
  95.     
  96.     public synchronized void receive(DatagramPacket p) throws IOException {
  97.         InetAddress peekAddress = new InetAddress();
  98.     
  99.         // peek at the packet to see who it is from.
  100.         int peekPort = datagramSocketPeek(peekAddress);
  101.  
  102.     // check the address is ok with the security manager before every recv.
  103.     SecurityManager security = System.getSecurityManager();
  104.     if (security != null) {
  105.        security.checkConnect(peekAddress.getHostName(), peekPort);
  106.     }
  107.     
  108.         // If the security check succeeds, then receive the packet.
  109.     datagramSocketReceive(p);
  110.  
  111.     return;
  112.     }
  113.  
  114.     /**
  115.      *    Returns the local port that this socket is bound to.
  116.      */
  117.     public int getLocalPort() {
  118.     return localPort;
  119.     }
  120.  
  121.     /**
  122.      * Close the datagram socket.
  123.      */
  124.     public synchronized void close() {
  125.     datagramSocketClose();
  126.     }
  127.  
  128.     protected synchronized void finalize() {
  129.     datagramSocketClose();
  130.     }
  131.  
  132.     private native void datagramSocketCreate();
  133.     private native int  datagramSocketBind(int port);
  134.     private native void datagramSocketSend(DatagramPacket p);
  135.     private native int datagramSocketPeek(InetAddress i);
  136.     private native void datagramSocketReceive(DatagramPacket p);
  137.     private native void datagramSocketClose();
  138. }
  139.